/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.router.core.domain.action;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import info.peigen.hotpot.business.router.core.domain.RouterTransLog;
import info.peigen.hotpot.business.router.core.domain.Withdraw;
import info.peigen.hotpot.business.router.core.domain.WithdrawFee;
import info.peigen.hotpot.business.router.core.repository.entity.RouterWithdrawEntity;
import info.peigen.hotpot.business.router.facade.Channel;
import info.peigen.hotpot.business.router.facade.enums.ChannelBusinessType;
import info.peigen.hotpot.business.router.facade.order.withdraw.detail.WithdrawDetail;
import info.peigen.hotpot.common.core.domain.order.Order;

import java.util.stream.Collectors;

/**
 * <b>(RouterWithdrawConverter)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/8/31
 */
public class RouterWithdrawConverter {
    public static RouterWithdrawEntity convert(Withdraw domain) {
        return BeanUtil.copyProperties(domain, RouterWithdrawEntity.class)
                .setWithdrawChannelCode(domain.getWithdrawChannel().getChannelCode())
                .setCurrency(domain.getTotalAmount().getCurrency())
                .setFee(JSON.toJSONString(domain.getFee()))
                .setDetails(JSON.toJSONString(domain.getDetails()))
                .setOrders(JSON.toJSONString(domain.getOrders()))
                .setPositionTransLogId(ObjectUtil.isNotEmpty(domain.getPositionTransLog()) ?
                        domain.getPositionTransLog().getTransLogId() : "")
                .setWithdrawTransLogIds(ObjectUtil.isNotEmpty(domain.getWithdrawTransLogs()) ?
                        CollUtil.join(domain.getWithdrawTransLogs().stream().map(RouterTransLog::getTransLogId)
                                .collect(Collectors.toList()), ",") : "");
    }

    public static Withdraw convert(RouterWithdrawEntity entity) {
        return BeanUtil.copyProperties(entity, Withdraw.class, "withdrawChannel", "totalAmount", "fee", "details", "orders")
                .setWithdrawChannel(Channel.genChannel(entity.getWithdrawChannelCode(), ChannelBusinessType.Withdraw))
                .setTotalAmount(entity.getTotalAmount().setCurrency(entity.getCurrency()))
                .setFee(JSON.parseObject(entity.getFee(), WithdrawFee.class))
                .setDetails(JSON.parseArray(entity.getDetails(), WithdrawDetail.class))
                .setOrders(JSON.parseArray(entity.getOrders(), Order.class));
    }
}
